﻿using System.Collections.Generic;
using Hidistro.Core;
using Hidistro.Core.Enums;
using Hidistro.Entities;
using Hidistro.Entities.Commodities;
using Hidistro.SaleSystem.Catalog;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using wnxd_feature;
using System;

namespace Hidistro.UI.SaleSystem.Tags
{
    public class wnxd_SubPage_sort_9 : WebControl
    {
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.RendHtml());
        }

        public string RendHtml()
        {
            StringBuilder builder = new StringBuilder();
            int categoryId = int.Parse((HttpContext.Current.Handler as System.Web.UI.Page).Request.QueryString["CategoryId"]);
            CategoryInfo category = CategoryBrowser.GetCategory(categoryId);
            List<CategoryInfo> list = CategoryBrowser.GetMaxSubCategories(category.CategoryId, 0x3e8) as List<CategoryInfo>;
            int i = 0;
            feature wnxd = new feature();
            foreach (CategoryInfo ca in list)
            {
                DataTable productList = this.GetRandomList(ca.CategoryId, 5);
                if ((productList != null) && (productList.Rows.Count > 0))
                {
                    builder.AppendFormat("<div class=\"title-js\">{0}", (++i) + "F —" + ca.Name);
                    builder.Append("<div class=\"title-js-one\">");
                    string[] wnxd_feature = wnxd.GetFeature(ca.CategoryId);
                    string url = Globals.GetSiteUrls().SubCategory(ca.CategoryId, ca.RewriteName, ca.ParentRewriteName);
                    for (int n = 0; n < wnxd_feature.Length; n++)
                    {
                        if (!string.IsNullOrEmpty(wnxd_feature[n]))
                        {
                            builder.AppendFormat("<a href=\"{1}\" target=\"_blank\">{0}</a>", wnxd_feature[n], url + "?feature=" + (n + 1));
                        }
                    }
                    builder.AppendFormat("</div><div class=\"title-js-two\">");
                    builder.AppendFormat("<a href=\"{0}\" target=\"_blank\"><img data-original=\"http://image.juseshop.com/UploadImage/AdvertImg/js-img92.jpg\" border=\"0\"></a>", Globals.GetSiteUrls().SubCategory(ca.CategoryId, ca.RewriteName, ca.ParentRewriteName));
                    builder.AppendFormat("</div></div>");
                    builder.AppendFormat("<div class=\"content8-js\">");
                    foreach (DataRow row in productList.Rows)
                    {
                        builder.AppendFormat("<div class=\"pro8\">");
                        string title = string.Empty;
                        string alt = string.Empty;
                        string keyword = row["Meta_Keywords"].ToString();
                        if (!string.IsNullOrEmpty(keyword))
                        {
                            string[] arr = keyword.Split('|');
                            if (arr.Length == 3)
                            {
                                title = arr[1];
                                alt = arr[2];
                            }
                        }
                        builder.AppendFormat("<a href=\"{0}\" target=\"_blank\"><img data-original=\"{1}\" width=\"230px\" height=\"473px\" title=\"{2}\" alt=\"{3}\"></a>", Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }), Globals.GetImgUrl(row["ImageUrl7"].ToString()), title, alt);
                        builder.AppendFormat("<div class=\"fl\"><div class=\"bg\"></div>");
                        builder.AppendFormat("<div class='wnxd-text'><p><a title=\"{0}\" href=\"{1}\" style=\"color:#fff\" target=\"_blank\">{0}</a>", row["ProductName"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                        if (row["ProductText"] != DBNull.Value) builder.AppendFormat("&nbsp;&nbsp;<a style=\"color:#F53864\" title=\"{0}\" href=\"{1}\" target=\"_blank\">{0}</a>", row["ProductText"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                        if (row["RankPrice"] == DBNull.Value) row["RankPrice"] = row["MarketPrice"];
                        builder.AppendFormat("</p><em>￥{0}</em></div>", Globals.FormatMoney((decimal)row["RankPrice"]));
                        builder.AppendFormat("</div></div>");
                    }

                    builder.AppendFormat("</div>");
                }
            }
            return builder.ToString();
        }
        private DataTable GetRandomList(int categoryid, int MaxNum)
        {
            SubjectListQuery query = new SubjectListQuery
            {
                SortBy = "VistiCounts",
                SortOrder = SortAction.Desc,
                CategoryIds = categoryid.ToString(),
                MaxNum = MaxNum
            };
            return ProductBrowser.GetSubjectList(query);
        }
    }
}
